<template>
    <div v-loading="loading">
        <div>
            <e-container>
                <e-module-model-box title="在线留言板">
                    <div class="list-table">
                        <table width="100%" border="1" class="table table-list table-bordered table-hover">
                            <thead>
                                <tr align="center">
                                    <th>姓名</th>
                                    <th>联系电话</th>
                                    <th>留言内容</th>
                                    <th>留言人</th>
                                    <th>状态</th>
                                    <th>回复内容</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for="(r,i) in list">
                                    <td>{{ r.xingming }}</td>
                                    <td>{{ r.lianxidianhua }}</td>
                                    <td>{{ r.liuyanneirong }}</td>
                                    <td>{{ r.liuyanren }}</td>
                                    <td>{{ r.zhuangtai }}</td>
                                    <td>{{ r.huifuneirong }}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <div style="margin-top: 10px; text-align: center">
                        <el-pagination @current-change="loadList" :current-page="page" :page-size="15" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
                        </el-pagination>
                    </div>
                </e-module-model-box>
            </e-container>
        </div>
    </div>
</template>
<style type="text/scss" scoped lang="scss"></style>
<script>
    import api from "@/api";
    import { extend } from "@/utils/extend";
    import objectDiff from "objectdiff";

    /**
     * 后台列表页面
     */
    export default {
        data() {
            return {
                loading: false,
                list: [],
                search: {
                    xingming: "",

                    lianxidianhua: "",

                    liuyanneirong: "",

                    liuyanren: "",

                    zhuangtai: "",

                    huifuneirong: "",
                },
                page: 1, // 当前页
                pagesize: 12, // 页大小
                totalCount: 0, // 总行数
                total: {},
            };
        },
        watch: {},
        computed: {},
        methods: {
            searchSubmit() {
                this.loadList(1);
            },
            loadList(page) {
                // 防止重新点加载列表
                if (this.loading) return;
                this.loading = true;
                this.page = page;
                // 筛选条件
                var filter = extend(true, {}, this.search, { page: page + "", pagesize: this.pagesize + "" });
                var diff = objectDiff.diff(filter, this.$route.query);
                if (diff.changed != "equal") {
                    // 筛选的条件不一致则更新链接
                    this.$router.replace({
                        // 更新query
                        path: this.$route.path,
                        query: filter,
                    });
                }
                this.$post(api.liuyanban.weblist, filter)
                    .then((res) => {
                        this.loading = false;
                        if (res.code == api.code.OK) {
                            extend(this, res.data);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                    .catch((err) => {
                        this.loading = false;
                        this.$message.error(err.message);
                    });
            },
        },
        beforeRouteUpdate(to, form, next) {
            var search = extend(this.search, to.query);
            if (search.page) {
                this.page = Math.floor(to.query.page);
                delete search.page;
            }
            if (search.pagesize) {
                this.pagesize = Math.floor(to.query.pagesize);
                delete search.pagesize;
            }
            this.loadList(1);
            next();
        },
        created() {
            var search = extend(this.search, this.$route.query);
            if (search.page) {
                this.page = Math.floor(this.$route.query.page);
                delete search.page;
            }
            if (search.pagesize) {
                this.pagesize = Math.floor(this.$route.query.pagesize);
                delete search.pagesize;
            }
            this.loadList(this.page);
        },
        mounted() {},
        destroyed() {},
    };
</script>
